#include "UModelLoader.h"
#include "UModel.h"
#include "UStringUtils.h"
#include <string>
#include <iostream>
#include <fstream>
#include <boost/date_time/posix_time/posix_time_types.hpp>
using namespace std;
namespace ptime = boost::posix_time;

TModel* TModelLoader::loadModelFromFile(string& filename) {
	return loadModelFromFile(filename.c_str());
}

TModel* TModelLoader::loadModelFromFile(const char* filename) {
	TModel * model = NULL;
	cerr << STRFORMAT(getClassName()+": Attempting to load '%s'\n",filename);
	ifstream ifs(filename, ifstream::in);

	ptime::ptime tstart;
	ptime::ptime tstop;

	if( ifs.good() ) {
		tstart = ptime::microsec_clock::local_time();
		model = loadModelFromStream(ifs);
		tstop = ptime::microsec_clock::local_time();
	}
	else
		cerr << STRFORMAT(getClassName()+": ERROR: Can not load data from '%s'\n", filename);
	ifs.close();

	ptime::time_duration dur = tstop-tstart;

	float secs = dur.total_nanoseconds()/1000000000.0;
	float prf = (model->vertexCount()/secs);
	cerr << STRFORMAT(getClassName()+": Loaded '%s', %lu vertices in %4.2f seconds (%4.2f vertices/second)\n", filename, model->vertexCount(),secs,prf);
	
	return model;
}
